home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / FastStringBuffer.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  10.2 KB  |  347 lines  |  [TEXT/CWIE]

  1. // FastStringBuffer.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. /**
  8.  * Object subclass resembling the java.lang.StringBuffer class (an object that
  9.  * manages a mutable string).  Unlike java.lang.StringBuffer, none of
  10.  * FastStringBuffer's methods are synchronized, which results in a significant
  11.  * performance increase.  FastStringBuffer also has additional API that allows
  12.  * it to be more easily modified than the standard StringBuffer.
  13.  * @note 1.0 fixes some error by 1 problems in insert() method
  14.  */
  15.  
  16.  
  17. class FastStringBuffer extends Object {
  18.     String      string;
  19.     char        buffer[];
  20.     int         length;
  21.     boolean     doublesCapacity;
  22.  
  23. /* constructors */
  24.  
  25.     /** Creates a FastStringBuffer containing the empty string.
  26.       */
  27.     public FastStringBuffer() {
  28.         this("");
  29.     }
  30.  
  31.     /** Creates a FastStringBuffer containing the characters in <b>aString</b>.
  32.       */
  33.     public FastStringBuffer(String aString) {
  34.         super();
  35.  
  36.         if (aString == null || aString.equals("")) {
  37.             buffer = new char[8];
  38.         } else {
  39.             buffer = new char[aString.length() + 1];
  40.             setStringValue(aString);
  41.         }
  42.         doublesCapacity=false;
  43.     }
  44.  
  45.     /** Creates a FastStringBuffer containing the characters in <b>aString</b>
  46.      *  from the index <b>start</b> to <b>end<\b>. <b>end<\b> is excluded.
  47.      */
  48.     public FastStringBuffer(String aString,int start,int end) {
  49.         int i;
  50.         buffer = new char[end - start];
  51.         length = end-start;
  52.         string = null;
  53.         doublesCapacity = false;
  54.         aString.getChars(start,end,buffer,0);
  55.     }
  56.  
  57.     /** Creates a FastStringBuffer containing the character <b>aChar</b>.
  58.       */
  59.     public FastStringBuffer(char aChar) {
  60.         super();
  61.  
  62.         buffer = new char[8];
  63.         buffer[0] = aChar;
  64.         length = 1;
  65.         doublesCapacity=false;
  66.     }
  67.  
  68.     void _increaseCapacityTo(int newCapacity) {
  69.         char          oldBuffer[];
  70.  
  71.         if (buffer.length <= newCapacity) {
  72.             oldBuffer = buffer;
  73.             if( doublesCapacity )
  74.               buffer = new char[newCapacity * 2];
  75.             else
  76.               buffer = new char[newCapacity + 20];
  77.             System.arraycopy(oldBuffer, 0, buffer, 0, oldBuffer.length);
  78.         }
  79.     }
  80.  
  81.     /** Set whether the FastStringBuffer should double its size
  82.      *  when some data is inserted and the internal buffer is
  83.      *  too small.
  84.      */
  85.     public void setDoublesCapacityWhenGrowing(boolean aFlag){
  86.         doublesCapacity = aFlag;
  87.     }
  88.  
  89.     /** Returns whether FastStringBuffer doubles its size when
  90.      *  some data is inserted and the internal buffer is
  91.      *  too small.
  92.      */
  93.     public boolean doublesCapacityWhenGrowing() {
  94.         return doublesCapacity;
  95.     }
  96.  
  97.     /** Sets the FastStringBuffer's contents to the characters in
  98.       * <b>aString</b>.
  99.       */
  100.     public void setStringValue(String aString) {
  101.         if (aString == null || aString.equals("")) {
  102.             length = 0;
  103.         } else {
  104.             length = aString.length();
  105.             _increaseCapacityTo(length);
  106.             aString.getChars(0, length, buffer, 0);
  107.         }
  108.  
  109.         string = aString;
  110.     }
  111.  
  112.     /** Returns the String for the FastStringBuffer's contents.
  113.       */
  114.     public String toString() {
  115.         if (string == null) {
  116.             string = new String(buffer, 0, length);
  117.         }
  118.  
  119.         return string;
  120.     }
  121.  
  122.     /** Returns the character at <b>index</b>.
  123.       * @exception StringIndexOutOfBoundsException If the index is invalid.
  124.       */
  125.     public char charAt(int index) {
  126.         if (index < 0 || index >= length) {
  127.             throw new StringIndexOutOfBoundsException(index);
  128.         }
  129.  
  130.         return buffer[index];
  131.     }
  132.  
  133.     /** Returns the index of the first occurrance of <b>aChar</b> in the
  134.       * FastStringBuffer, starting at character <b>offset</b>.
  135.       * @exception StringIndexOutOfBoundsException If the offset is invalid.
  136.       */
  137.     public int indexOf(char aChar, int offset) {
  138.         int     i;
  139.  
  140.         if (offset < 0 || offset >= length) {
  141.             throw new StringIndexOutOfBoundsException(offset);
  142.         }
  143.  
  144.         for (i = offset; i < length; i++) {
  145.             if (buffer[i] == aChar) {
  146.                 return i;
  147.             }
  148.         }
  149.  
  150.         return -1;
  151.     }
  152.  
  153.     /** Returns the index of the first occurrance of <b>aChar</b> in the
  154.       * FastStringBuffer.  Equivalent to the code:
  155.       * <pre>
  156.       *     indexOf(aChar, 0);
  157.       * </pre>
  158.       * @see #indexOf
  159.       */
  160.     public int indexOf(char aChar) {
  161.         return indexOf(aChar, 0);
  162.     }
  163.  
  164.     /** Returns <b>true</b> if the FastStringBuffer contains a space or tab
  165.       * character at position <b>index</b>.
  166.       * @exception StringIndexOutOfBoundsException If the index is invalid.
  167.       */
  168.     public boolean tabOrSpaceAt(int index) {
  169.         if (index < 0 || index >= length) {
  170.             throw new StringIndexOutOfBoundsException(index);
  171.         }
  172.  
  173.         return (buffer[index] == ' ' || buffer[index] == '\t');
  174.     }
  175.  
  176.     /** Appends <b>aChar</b> to the FastStringBuffer.
  177.       */
  178.     public void append(char aChar) {
  179.         _increaseCapacityTo(length + 1);
  180.         buffer[length++] = aChar;
  181.         string = null;
  182.     }
  183.  
  184.     /** Appends <b>aString</b> to the FastStringBuffer.
  185.       */
  186.     public void append(String aString) {
  187.         if (aString == null || aString.equals("")) {
  188.             return;
  189.         }
  190.  
  191.         _increaseCapacityTo(length + aString.length());
  192.         aString.getChars(0, aString.length(), buffer, length);
  193.         length += aString.length();
  194.         string = null;
  195.     }
  196.  
  197.     /** Inserts <b>aChar</b> at <b>index</b>.  If <b>index</b> is
  198.       * greater than or equal to the number of characters within the buffer,
  199.       * appends <b>aChar</b>.
  200.       * @exception StringIndexOutOfBoundsException if the index is invalid.
  201.       */
  202.     public void insert(char aChar, int index) {
  203.         char    oldBuffer[];
  204.         int     i;
  205.  
  206.         if (index < 0) {
  207.             throw new StringIndexOutOfBoundsException(index);
  208.         } else if (index >= length) {
  209.             append(aChar);
  210.             return;
  211.         }
  212.  
  213.         if (length < buffer.length) {
  214.             if (index != length) {
  215.                 System.arraycopy(buffer, index, buffer, index + 1,
  216.                                  length - index);
  217.             }
  218.             buffer[index] = aChar;
  219.             length++;
  220.             string = null;
  221.             return;
  222.         }
  223.  
  224.         oldBuffer = buffer;
  225.         buffer = new char[buffer.length + 20];
  226.         if (index > 0) {
  227.             System.arraycopy(oldBuffer, 0, buffer, 0, index);
  228.         }
  229.         if (index != length ) {
  230.             System.arraycopy(oldBuffer, index, buffer, index + 1,
  231.                              length - index);
  232.         }
  233.         buffer[index] = aChar;
  234.         length++;
  235.  
  236.         string = null;
  237.     }
  238.  
  239.     /** Inserts <b>aString</b> at <b>index</b>.  If <b>index</b> is
  240.       * greater than or equal to the number of characters within the buffer,
  241.       * appends <b>aString</b>.
  242.       * @exception StringIndexOutOfBoundsException If the index is invalid.
  243.       */
  244.         public void insert(String aString, int index) {
  245.         char    oldBuffer[];
  246.         int     stringLength, i;
  247.  
  248.         if (index < 0) {
  249.             throw new StringIndexOutOfBoundsException(index);
  250.         } else if (index > length) {
  251.             append(aString);
  252.             return;
  253.         } else if (aString == null || aString.equals("")) {
  254.             return;
  255.         }
  256.  
  257.         stringLength = aString.length();
  258.         if (length + stringLength < buffer.length) {
  259.             System.arraycopy(buffer, index, buffer, index + stringLength,
  260.                              length - index);
  261.             aString.getChars(0, stringLength, buffer, index);
  262.             length += stringLength;
  263.             string = null;
  264.             return;
  265.         }
  266.  
  267.         oldBuffer = buffer;
  268.         buffer = new char[length + stringLength + 20];
  269.         if (index > 0) {
  270.             System.arraycopy(oldBuffer, 0, buffer, 0, index);
  271.         }
  272.         System.arraycopy(oldBuffer, index, buffer, index + stringLength,
  273.                          length - index);
  274.         aString.getChars(0, stringLength, buffer, index);
  275.         length += stringLength;
  276.  
  277.         string = null;
  278.     }
  279.  
  280.     /** Removes the character at <b>index</b>.
  281.       * @exception StringIndexOutOfBoundsException if the index is invalid.
  282.       */
  283.     public void removeCharAt(int index) {
  284.         if (index < 0 || index >= length) {
  285.             throw new StringIndexOutOfBoundsException(index);
  286.         }
  287.  
  288.         if (index + 1 == length) {
  289.             length--;
  290.             string = null;
  291.             return;
  292.         }
  293.  
  294.         System.arraycopy(buffer, index + 1, buffer, index, length - (index+1));
  295.         length--;
  296.  
  297.         string = null;
  298.     }
  299.  
  300.     /** Truncates the FastStringBuffer to <b>aLength</b> characters.  If
  301.       * <b>aLength</b> is invalid, does nothing.
  302.       */
  303.     public void truncateToLength(int aLength) {
  304.         if (aLength < 0 || aLength > length) {
  305.             return;
  306.         }
  307.  
  308.         length = aLength;
  309.  
  310.         string = null;
  311.     }
  312.  
  313.     /** Returns the number of characters in the FastStringBuffer.
  314.       */
  315.     public int length() {
  316.         return length;
  317.     }
  318.  
  319.     /** Returns the number of characters in the FastStringBuffer.
  320.       */
  321.     public void moveChars(int fromIndex, int toIndex) {
  322.         if (fromIndex <= toIndex) {
  323.             return;
  324.         } else if (fromIndex < 0 || fromIndex >= length) {
  325.             throw new StringIndexOutOfBoundsException(fromIndex);
  326.         } else if (toIndex < 0 || toIndex >= length) {
  327.             throw new StringIndexOutOfBoundsException(toIndex);
  328.         }
  329.  
  330.         System.arraycopy(buffer, fromIndex, buffer, toIndex,
  331.                          length - fromIndex);
  332.         length -= fromIndex - toIndex;
  333.  
  334.         string = null;
  335.     }
  336.  
  337.     /** Returns the FastStringBuffer's char array, for situation where it is
  338.       * needed.  For example, you can draw the FastStringBuffer's contents
  339.       * by passing the array to the Graphic's <b>drawString()</b> method that
  340.       * takes a char array, rather than first convert the StringBuffer to a
  341.       * String.  You should never modify this array yourself.
  342.       */
  343.     public char[] charArray() {
  344.         return buffer;
  345.     }
  346. }
  347.